﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using Commerce.Common;

namespace Commerce.Promotions
{
    public class FreeShippingOver : Coupon
    {
        public FreeShippingOver()
            : base()
        {
        }

        public FreeShippingOver(string couponCode, CouponType couponType)
            : base(couponCode, couponType)
        {
        }
        private int _orderMoreThan;

        public int FreeShippingLimit
        {
            get { return _orderMoreThan; }
            set { _orderMoreThan = value; }
        }

        private string _moreThanType;

        [SelectorType("CouponTypeSelector", typeList = "dollars,items")]
        public string MoreThanType
        {
            get { return _moreThanType; }
            set { _moreThanType = value; }
        }

        private string _noactivationRequired;

        [SelectorType("CouponTypeSelector", typeList = "No,Yes", automatic = true)]
        public string AutomaticallyApplied
        {
            get { return _noactivationRequired; }
            set { _noactivationRequired = value; }
        }

        private string _applyOtherCoupons;

        [SelectorType("CouponTypeSelector", typeList = "No,Yes")]
        public string ApplyOtherCoupons
        {
            get { return _applyOtherCoupons; }
            set { _applyOtherCoupons = value; }
        }

        decimal CalculateDiscountAmount(decimal itemAmount)
        {

            decimal result = 0;
            return result;
        }

        public override bool ApplyCouponToOrder(Commerce.Common.Order order)
        {
            //Validate the order to make sure it's good.  
            CouponValidationResponse validationResponse = ValidateCouponForOrder(order);

            if (!validationResponse.IsValid)
            {
                throw new ArgumentException("Coupon is not valid for order: " + validationResponse, "order");
            }

            XDocument doc = XDocument.Parse(base.GetXmlData());
            XElement element = doc.Descendants("AutomaticallyApplied").SingleOrDefault();

            if (element.Value == "Yes")
            {
                if (order.CalculateSubTotal() >= FreeShippingLimit && AutomaticallyApplied == "Yes")
                {
                    order.ShippingAmount = 0;
                    return true;
                }
            }

            return false;
        }
    }
}
